package com.ruoyi.zcxt.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zcxt.constant.ZcxtConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zcxt.mapper.ZcClassifyMapper;
import com.ruoyi.zcxt.domain.ZcClassify;
import com.ruoyi.zcxt.service.IZcClassifyService;

/**
 * 资产分类（树）Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-06
 */
@Service
public class ZcClassifyServiceImpl implements IZcClassifyService 
{
    @Autowired
    private ZcClassifyMapper zcClassifyMapper;

    /**
     * 查询资产分类（树）
     * 
     * @param cfId 资产分类（树）主键
     * @return 资产分类（树）
     */
    @Override
    public ZcClassify selectZcClassifyByCfId(Long cfId)
    {
        return zcClassifyMapper.selectZcClassifyByCfId(cfId);
    }

    /**
     * 查询资产分类（树）列表
     * 
     * @param zcClassify 资产分类（树）
     * @return 资产分类（树）
     */
    @Override
    public List<ZcClassify> selectZcClassifyList(ZcClassify zcClassify)
    {
        return zcClassifyMapper.selectZcClassifyList(zcClassify);
    }

    /**
     * 新增资产分类（树）
     * 
     * @param zcClassify 资产分类（树）
     * @return 结果
     */
    @Override
    public int insertZcClassify(ZcClassify zcClassify)
    {
        ZcClassify info = zcClassifyMapper.selectZcClassifyByCfId(zcClassify.getParentId());
        if (info == null && zcClassify.getParentId() == 0){
            zcClassify.setAncestors("0");
        }else {
            if (!ZcxtConstants.CLASSIFY_NORMAL.equals(info.getStatus())){
                throw new ServiceException("分类停用，不允许新增");
            }
            zcClassify.setAncestors(info.getAncestors() + "," + zcClassify.getParentId());
        }
        zcClassify.setCreateTime(DateUtils.getNowDate());
        return zcClassifyMapper.insertZcClassify(zcClassify);
    }

    /**
     * 修改资产分类（树）
     * 
     * @param zcClassify 资产分类（树）
     * @return 结果
     */
    @Override
    public int updateZcClassify(ZcClassify zcClassify)
    {
        ZcClassify newParentClassify = zcClassifyMapper.selectZcClassifyByCfId(zcClassify.getParentId());
        ZcClassify oldClassify = zcClassifyMapper.selectZcClassifyByCfId(zcClassify.getCfId());
        if (StringUtils.isNotNull(newParentClassify)  && StringUtils.isNotNull(oldClassify)) {
            String newAncestors = newParentClassify.getAncestors() + "," + newParentClassify.getCfId();
            String oldAncestors = oldClassify.getAncestors();
            zcClassify.setAncestors(newAncestors);
            updateZcClassifyChildren(zcClassify.getCfId(), newAncestors, oldAncestors);
        }
        int result = zcClassifyMapper.updateZcClassify(zcClassify);
        if (ZcxtConstants.CLASSIFY_NORMAL.equals(zcClassify.getStatus()) && StringUtils.isNotEmpty(zcClassify.getAncestors())
                && !StringUtils.equals("0",zcClassify.getAncestors()))
        {
            // 如果该分类是启用状态，则启用该分类的所有上级分类
            updateParentZcClassifyStatusNormal(zcClassify);
        }
        zcClassify.setUpdateTime(DateUtils.getNowDate());
        return result;
    }

    /**
     * 批量删除资产分类（树）
     * 
     * @param cfIds 需要删除的资产分类（树）主键
     * @return 结果
     */
    @Override
    public int deleteZcClassifyByCfIds(Long[] cfIds)
    {
        return zcClassifyMapper.deleteZcClassifyByCfIds(cfIds);
    }

    /**
     * 删除资产分类（树）信息
     * 
     * @param cfId 资产分类（树）主键
     * @return 结果
     */
    @Override
    public int deleteZcClassifyByCfId(Long cfId)
    {
        return zcClassifyMapper.deleteZcClassifyByCfId(cfId);
    }

    @Override
    public int selectNormalChildrenCfById(Long cfId) {
        return zcClassifyMapper.selectNormalChildrenCfById(cfId);
    }

    /**
     * 修改子元素关系
     */
    public void updateZcClassifyChildren(Long cfId, String newAncestors, String oldAncestors){
        List<ZcClassify> children = zcClassifyMapper.selectChildrenClassifyByCfId(cfId);
        for (ZcClassify child : children) {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0) {
            zcClassifyMapper.updateZcClassifyChildren(children);
        }
    }

    public void  updateParentZcClassifyStatusNormal(ZcClassify zcClassify) {
        String ancestors = zcClassify.getAncestors();
        Long[] cfIds = Convert.toLongArray(ancestors);
        zcClassifyMapper.updateZcClassifyStatusNormal(cfIds);
    }
}
